var isTimeTableOk = false;//课表结构是否设置完毕
var weekDayCn = ["一", "二", "三", "四", "五", "六", "日"];
var isDeleted = false;//是否在规则列表中删除单个规则
var preScheduleList;
var preScheduleMap;
var lessonsPerDay;
var defaultDaysPerWeek = 5;//默认每周的上课天数
var defaultAmLessonCount = 4;//默认每天上午的课时数
var defaultPmLessonCount = 3;//默认每天下午的课时数
var defaultEveLessonCount = 0;//默认每天晚上的课时数
var isDeleted = false;//是否在规则列表中删除单个规则

/**
 * Loading
 */

$(function () {
    setTaskStep("rule");
    setTaskRuleStep("task-rule-unScheduleTime");
    $("#ruleListDiv").css("display", "block");
    //获取课表结构
    getTimeTableStructure();
    //初始化班级不排课
    classUnSchedule();
    //加载预排课程
    getPreScheduleCCT();
    //加载tag信息
    getTagRuleMap();
    getTags();

    //$("#tagArea").delegate("td","click",function(){
    //    if($(this).hasClass("tagActive")){  //取消tag
    //        $(this).removeClass("tagActive");
    //        selectedTagId = "";
    //        selectedTagName = "";
    //    }else{
    //        $("#tagArea td").removeClass("tagActive");
    //        $(this).addClass("tagActive");
    //        selectedTagId = $(this).attr('id');
    //        selectedTagName = $(this).text();
    //    }
    //});
    $("#tagArea").delegate("button", "click", function () {
        if ($(this).hasClass("btn-primary")) {  //取消tag
            $(this).removeClass("btn-primary");
            selectedTagId = "";
            selectedTagName = "";
        } else {
            $("#tagArea button").removeClass("btn-primary");
            $(this).addClass("btn-primary");
            selectedTagId = $(this).attr('id');
            selectedTagName = $(this).text();
        }
    });
    //prevent the "enter" key event
    $("#addTagsModal").keydown(function (e) {
        if (e.keyCode == "13") {
            tagSubmit();
            return false;
        }
    });

    $("#updateTagsModal").on("hidden.bs.modal", function () {
        console.log("update modal hidden event ...");
        console.log("tagChanged : " + tagChanged);
        if (tagChanged == true) {
            reloadPage();
        }
    });
})

/**
 * 加载预排课程
 */
function getPreScheduleCCT() {
    jQuery.ajax({
        url: "/m/courseplan/getPreScheduleList",
        data: {"taskId": taskId},
        type: "GET",
        cache: false,
        success: function (ret) {
            if (ret) {
                preScheduleMap = eval("(" + ret + ")");
            } else {
                preScheduleMap = "";
            }
        }
    });
}
/**
 * 返回此lessonTime上的预排的课程
 * @param lessonTime
 * @returns {*}
 */
function isConflictWithPreSchedule(lessonTime) {

    if (preScheduleMap == "") {
        return false;
    }
    for (var key in preScheduleMap) {
        if (key == lessonTime) {
            return preScheduleMap[key];
        }
    }
    return false;
}
/*切换时清除td内容*/
function clearTimeTable() {

    $(".table-header-item input").each(function () {
        var $this = $(this);
        $this.prop("checked", false);
    })

    for (var lessonTime = 0; lessonTime < parseInt(defaultDaysPerWeek)*(parseInt(defaultAmLessonCount)+parseInt(defaultPmLessonCount) + parseInt(defaultEveLessonCount)); lessonTime++) {
        var $col = $("#td_" + lessonTime);
        $col.removeClass("disabledTd");
        $col.removeAttr("disabled");
        $col.removeAttr("ctype");
        $col.html("&nbsp;");
    }
    /*
    $("#rightTable_body tr td").each(
        function () {
            var $this = $(this);
            var text = $this.text();
            if (text.length > 0 && !jQuery.isNumeric(text)) {   // 有内容并且不是数字 就清理干净
                $this.html("&nbsp;");
            }
            if ($this.hasClass("disabledTd")) {
                $this.removeClass("disabledTd");
                $this.removeAttr("disabled");
                $this.html("&nbsp;");
            }
        });*/


    clearTagInfo();


}

/*region 左侧导航相关*/
/*展开、关闭年级班级或课程年级树*/
function toggleTree(parentId) {
    $("#" + parentId).children("li").toggle();
    var icon = $("#" + parentId).find("i");
    if (icon.hasClass('glyphicon-plus-sign')) {
        icon.removeClass('glyphicon-plus-sign').addClass('glyphicon-minus-sign');
    } else {
        icon.removeClass('glyphicon-minus-sign').addClass('glyphicon-plus-sign');
    }
}

/*设置不排课规则类型*/
function setRuleType() {
    var ruleType = $("#unScheduleRuleTypeSlt").val();
    if (unScheduleRuleType != ruleType) {
        alertInfo("查询中...");
        //先保存当前的不排课规则
        generateResult();
        //清空表格
        clearTimeTable();
        //清空选中节点值
        selectedNodeType = "";
        selectedNodeValue = "";

        //获取新的排课规则的数据
        unScheduleRuleType = ruleType;
        //显示左侧导航
        showMainNav();
        //加载数据
        if (unScheduleRuleType == 'class') {
            classUnSchedule();
        } else if (unScheduleRuleType == 'course') {
            courseUnSchedule();
        } else {
            teacherUnSchedule();
        }
    }
}

//切换显示左侧导航
function showMainNav() {
    $("div[id*='mainNav_']").hide();
    $("#mainNav_" + unScheduleRuleType).show();
}

/*关闭所有子节点*/
function closeAllChildren() {
    var root = $("#mainNav_" + unScheduleRuleType);
    root.children("ul").children("li").hide();
}

/*endregion 左侧导航相关*/

///*region 设置课表结构相关*/
///*显示设置课表结构modal*/
//function showTimeTableModal() {
//    //每次点击节次设置 都得先保存数据 不然会出现错误
//    generateResult();
//    $('#myModal').modal('show');
//}

/*从后台获取课表结构*/
function getTimeTableStructure() {
    jQuery.ajax({
        type: "GET",
        url: "getTimeTableStructure?taskId=" + taskId,
        async: false,
        cache: false,
        success: function (msg) {
            var message = JSON.parse(msg);
            var retData = message.data;
            if (retData != null) {
                isTimeTableOk = true;
                defaultDaysPerWeek = retData.daysPerWeek;
                defaultAmLessonCount = retData.amLessonCount;
                defaultPmLessonCount = retData.pmLessonCount;
                defaultEveLessonCount = retData.eveLessonCount;
                lessonsPerDay = parseInt(defaultAmLessonCount) + parseInt(defaultPmLessonCount) + parseInt(defaultEveLessonCount);
            }
            /*初始化课表结构下拉框*/
            // initTableStructureSlt();
            /*动态构建课表样式*/
            buildTimeTableStructure();
        }
    });
}

///*将课表结构数据写入后台，同时清除之前设置的所有规则*/
//function setTimeTableStructure(daysPerWeek, amLessonCount, pmLessonCount, eveLessonCount) {
//    var ret = false;
//    var jsonArray = {
//        "daysPerWeek": daysPerWeek, "amLessonCount": amLessonCount,
//        "pmLessonCount": pmLessonCount, "eveLessonCount": eveLessonCount,
//        "DoubleLesson": 0
//    };
//    var postData = {
//        "taskId": taskId,
//        "json": JSON.stringify(jsonArray)
//    };
//    jQuery.ajax({
//        type: "POST",
//        url: "setTimeTableStructure",
//        cache: false,
//        async: false,
//        data: postData,
//        success: function (msg) {
//            var message = JSON.parse(msg);
//            if (message != null) {
//                ret = message;
//            }
//        }
//    });
//    return ret;
//}


/*动态构建课表结构样式*/
function buildTimeTableStructure() {
    ////得到用户选择的课表结构
    var daysPerWeek = parseInt(defaultDaysPerWeek);
    var amLessonCount = parseInt(defaultAmLessonCount);
    var pmLessonCount = parseInt(defaultPmLessonCount);
    var eveLessonCount = parseInt(defaultEveLessonCount);

    buildTimeTable(daysPerWeek, amLessonCount, pmLessonCount, eveLessonCount);

}

function buildTimeTable(daysPerWeek, amLessonCount, pmLessonCount, eveLessonCount) {
    var timeIndex = 0;
    var headHtml = new Array();
    headHtml.push("<td width='8%'>&nbsp;</td>");
    var td_width_ave_percentage = 92 / daysPerWeek;
    /*
    for (var i = 0; i < daysPerWeek; i++) {
        headHtml.push("<td width='" + td_width_ave_percentage + "%'>周" + weekDayCn[i] + "</td>");
    }*/
    for (var i = 0; i < daysPerWeek; i++) {
        var htmlArr = [];
        var arr = [amLessonCount, "上午", pmLessonCount, "下午", eveLessonCount, "晚上"];
        for (var j = 0; j < arr.length; j = j + 2) {
            var period = j / 2;
            // 有课
            if (arr[j] > 0) {
                var text = arr[j + 1] + "不排";
                htmlArr.push("<div class='table-header-item'>" +
                    "<label><input type='checkbox' id='unScheduleAll_" + i + "_" + period + "'" +
                    " onclick='unScheduleAll(this, " + i + "," + period + ")'>" + text + "</label></div>");
            }
        }
        headHtml.push("<td width='" + td_width_ave_percentage + "%'><div class='float-left table-header-day-of-week'>周" + weekDayCn[i] + "</div>" +
            "<div class='float-left'>" + htmlArr.join("") + "</div></td>");
    }
    $("#rightTable_head").html(headHtml.join(""));


    headHtml = new Array();
    for (var i = 0; i < amLessonCount; i++) {
        if (i == (amLessonCount - 1)) {
            headHtml.push("<tr style='border-bottom:2px solid #ccc'>");
        } else {
            headHtml.push("<tr>");
        }
        headHtml.push("<td>" + (i + 1) + "</td>");
        for (var j = 0; j < daysPerWeek; j++) {
            timeIndex = timeIndex = j * (amLessonCount + pmLessonCount + eveLessonCount) + i;
            headHtml.push("<td class='timetable_avail_td' id=" + "td_" + timeIndex + " timeIndex=" + timeIndex + ">&nbsp;</td>");
        }
        headHtml.push("</tr>");
    }
    if (pmLessonCount > 0) {
        for (var i = amLessonCount; i < amLessonCount + pmLessonCount; i++) {
            if (i == (amLessonCount + pmLessonCount - 1)) {
                headHtml.push("<tr style='border-bottom:2px solid #ccc'>");
            } else {
                headHtml.push("<tr>");
            }
            headHtml.push("<td>" + (i + 1) + "</td>");
            for (var j = 0; j < daysPerWeek; j++) {
                timeIndex = timeIndex = j * (amLessonCount + pmLessonCount + eveLessonCount) + i;
                headHtml.push("<td class='timetable_avail_td' id=" + "td_" + timeIndex + " timeIndex=" + timeIndex + ">&nbsp;</td>");
            }
            headHtml.push("</tr>");
        }
    }
    if (eveLessonCount > 0) {
        //headHtml.push("<tr><td disabled colspan='" + (daysPerWeek + 1) + "'>&nbsp;</td></tr>");
        for (var i = amLessonCount + pmLessonCount; i < amLessonCount + pmLessonCount + eveLessonCount; i++) {
            headHtml.push("<tr>");
            headHtml.push("<td>" + (i + 1) + "</td>");
            for (var j = 0; j < daysPerWeek; j++) {
                timeIndex = timeIndex = j * (amLessonCount + pmLessonCount + eveLessonCount) + i;
                headHtml.push("<td class='timetable_avail_td' id=" + "td_" + timeIndex + " timeIndex=" + timeIndex + ">&nbsp;</td>");
            }
            headHtml.push("</tr>");
        }
    }
    $("#rightTable_body").html(headHtml.join(""));

    //隐藏modal
    if (isTimeTableOk == true) {
        $('#myModal').modal('hide');
    }

    if(!isMyTask){
        return;
    }
    // 为td绑定事件
    $("#rightTable_body tr td").each(function () {
        var td = $(this);
        $(td).on('click', function () {
            var timeIndexStr = $(this).attr('timeIndex');
            if (typeof timeIndexStr !== typeof undefined && timeIndexStr !== false) {
                var timeIndex = parseInt(timeIndexStr);
                setUnschedule(timeIndex);
                // var text = trim(td.text());
                // //if (td.text() == '不排课') {
                // if (text.length > 0) {  // 如果格子里面有字，不排课或者tag，则去掉不排课的同时，顺便去掉tag记录
                //     if (setRule(timeIndex, false, selectedTagId, selectedTagName)) {
                //         td.html("&nbsp;");
                //     }
                //     return;//不加出bug
                // } else {  //加入不排课规则的同时 添加tag记录
                //     if (setRule(timeIndex, true, selectedTagId, selectedTagName)) {
                //         if (selectedTagName != "") {
                //             td.html(selectedTagName);
                //         } else {
                //             td.html("不排课");
                //         }
                //     }
                // }
            }
        });
    });
    //隐藏modal
    if (isTimeTableOk == true) {
        $('#myModal').modal('hide');
    }
}
/*endregion 设置课表结构相关*/



/*从此步重新开始*/
function reStartFromHere(where) {
    var ret = reStartFromWhere(where);
    if (ret == true) {
        alertSuccess("设置成功！");
    } else {
        alertWarning("设置失败！");
    }
    reloadPage();
}


/*region 设置规则生成结果相关*/
/*点击课表设置不排课规则*/
function generateWarningInfoForSubclassConflict(gradeId, classIds) {
    var gradeName = idNameMap.get(gradeId);
    var len = classIds.length;
    var res = new Array();
    for (var i = 0; i < len; i++) {
        res.push("<tr>");
        res.push("<td>" + gradeName + "</td>");
        res.push("<td>" + idNameMap.get(classIds[i]) + "</td>");
        res.push("<td> 班级不排课 </td>");
        res.push("</tr>");
    }
    return res.join("");
}
function setRule(timeIndex, isUnSchedule, selectedTagId, selectedTagName) {
    var ret = true;
    timeIndex = timeIndex + "";
    if (isUnSchedule) { //要设置不排课  且 这个lessontTime的预排和不排课冲突 ，则显示提示信息！
        var preList = isConflictWithPreSchedule(timeIndex);
        if (preList && preList.length > 0) {
            var content = generateWarningInfo(preList, selectedNodeType, selectedNodeValue, timeIndex);
            if (content.length > 0) {
                $("#preListModal").modal('show');
                var title = "星期" + weekDayCn[parseInt(timeIndex / lessonsPerDay)] + "第" + (timeIndex % lessonsPerDay + 1 ) + "节";
                $("#preListModalTitle").html(title);
                $("#preListModalTable").html(content);
                return;
            }
        }
    }
    if (selectedNodeType == "grade") {
        if (isUnSchedule) { //  检测是否有子班级在此时间已经设置不排课，有则显示冲突提示
            var conflictList = isConflictWithSubclass(selectedNodeValue, timeIndex);
            if (conflictList != null && conflictList.length > 0) {
                var title = "星期" + weekDayCn[parseInt(timeIndex / lessonsPerDay)] + "第" + (timeIndex % lessonsPerDay + 1 ) + "节";
                var content = generateWarningInfoForSubclassConflict(selectedNodeValue, conflictList);
                $("#subClassListModal").modal("show");
                $("#subClassListModalTitle").html(title);
                $("#subClassListModalTable").html(content);
                return;
            }
        }
        if (isUnSchedule == true && !lessonCountCheck(selectedNodeValue,false)) { //课时数不够了
            alertWarning("可用课时数即将不足! 此年级所需总课时为 :" + classLessonCountMap.get(selectedNodeValue));
            return;
        }
        setGradeRule(selectedNodeValue, timeIndex, isUnSchedule);
        setGradeTagRule(selectedNodeValue, timeIndex, isUnSchedule, selectedTagId, selectedTagName);

    } else if (selectedNodeType == "class") {
        if ($("#td_" + timeIndex).hasClass("disabledTd")) {
            alertWarning("您已选择年级在此不排课！");
            ret = false;
        } else {
            if (isUnSchedule == true && !lessonCountCheck(selectedNodeValue,true)) { //课时数不够了
                alertWarning("可用课时数即将不足! 此班级所需总课时为 :" + classLessonCountMap.get(selectedNodeValue));
                ret = false;
            } else {
                setClassRule(selectedNodeValue, timeIndex, isUnSchedule);
                setClassTagRule(selectedNodeValue, timeIndex, isUnSchedule, selectedTagId, selectedTagName);
            }

        }
    } else if (selectedNodeType == "course") {
        setCourseRule(selectedNodeValue, timeIndex, isUnSchedule);
        setCourseTagRule(selectedNodeValue, timeIndex, isUnSchedule, selectedTagId, selectedTagName);

    } else if (selectedNodeType == "courseGrade") {
        if ($("#td_" + timeIndex).hasClass("disabledTd")) {
            alertWarning("您已选择课程在此不排课！");
            ret = false;
        } else {
            setCourseGradeRule(selectedNodeValue, timeIndex, isUnSchedule);
            setCourseGradeTagRule(selectedNodeValue, timeIndex, isUnSchedule, selectedTagId, selectedTagName);

        }
    } else if (selectedNodeType == "teacher") {
        setTeacherRule(selectedNodeValue, timeIndex, isUnSchedule);
        setTeacherTagRule(selectedNodeValue, timeIndex, isUnSchedule, selectedTagId, selectedTagName);
    } else {
        ret = false;
        alertWarning("请先选择左侧节点！");
    }

    if (ret == true) {
        $("#saveBtn").removeClass('disabled');
    }
    return ret;
}
/**
 * 显示此课时已经安排的预排结果
 * @param selectedNodeType
 * @param selectedNodeValue
 * @param timeIndex
 */
function generateWarningInfo(preList, selectedNodeType, selectedNodeValue, timeIndex) {
    var tableBody = new Array();
    for (var i = 0; i < preList.length; i++) {
        var item = "";
        if (selectedNodeType == "grade") {
            if (selectedNodeValue == preList[i].gradeId) {
                item = preList[i];
            }
        } else if (selectedNodeType == "class") {
            if (selectedNodeValue == preList[i].classId) {
                item = preList[i];
            }
        } else if (selectedNodeType == "course") {
            if (selectedNodeValue == preList[i].courseId) {
                item = preList[i];
            }
        } else if (selectedNodeType == "courseGrade") {
            if (selectedNodeValue == (preList[i].courseId + "_" + preList[i].gradeId)) {
                item = preList[i];
            }
        } else if (selectedNodeType == "teacher") {
            if (selectedNodeValue == preList[i].teacherId) {
                item = preList[i];
            }
        }
        if (item == "") continue;
        tableBody.push("<tr> <td>" + item.gradeName + "</td>")
        tableBody.push(" <td>" + item.className + "</td>")
        tableBody.push(" <td>" + item.teacherName + "</td>")
        tableBody.push(" <td>" + item.courseName + "</td> </tr>")
    }
    return tableBody.join("");
}


/*生成最终结果并保存*/
function generateResult() {
    if(!isMyTask){
        return;
    }
    //有修改才保存
    if (!$("#saveBtn").hasClass("disabled")) {
        $("#saveBtn").addClass('disabled');
        var postData = "";
        var res = "";
        var msg = "";
        if (unScheduleRuleType == "class") {
            //分别保存班级和年级规则数据
            res = toClassString();
            postData = {"taskId": taskId, "unScheduleType": unScheduleRuleType, "json": res};
            msg = saveData(postData);    // 这里会导致弹出两次提示信息
            if (msg == true) {
                res = toGlobalClassString();
                postData = {
                    "taskId": taskId,
                    "unScheduleType": "grade",
                    "json": res
                };
                msg = saveData(postData);
            }
        } else if (unScheduleRuleType == "course") {
            //分别保存课程年级和课程数据
            res = toCourseGradeString();
            postData = {"taskId": taskId, "unScheduleType": "course_grade", "json": res};
            msg = saveData(postData);
            if (msg == true) {
                res = toCourseString();
                postData = {
                    "taskId": taskId,
                    "unScheduleType": unScheduleRuleType ,
                    "json": res
                };
                saveData(postData);
            }
        } else {
            res = toTeacherString();
            postData = {"taskId": taskId, "unScheduleType": unScheduleRuleType, "json": res};
            msg = saveData(postData);
        }
        //这里判断 tagRule有没有发生改变
        //如果发生改变，就将数据发回去
        if (msg == true) {
            saveTagRule(unScheduleRuleType);
            alertSuccess("保存成功！");

        } else {
            alertWarning("保存失败! ");
        }
    }
}

/*最终保存结果的方法*/
function saveData(postData) {
    var message = false;
    jQuery.ajax({
        type: "POST",
        url: "setUnScheduleTime",
        async: false,
        data: postData,
        success: function (msg) {
            message = JSON.parse(msg);
        }
    });
    return message;
}

/*用户关闭页面前自动保存*/
$(window).on('beforeunload', function () {
    generateResult();
});
/*endregion 设置规则生成结果相关*/

function reloadPage() {
    location.reload();
    $("#unScheduleRuleTypeSlt").val('class');
    isDeleted = false;
}

// 添加、复用弹出窗口居中
$('#myModal').on('show.bs.modal', function (e) {
    $(this).find('.modal-dialog').css({
        'margin-top': function () {
            var modalHeight = 350; // $('#myModal').find('.modal-dialog').height();
            return ($(window).height() / 2 - (modalHeight / 2));
        }
    });
});

function trim(x) {
    return x.replace(/^\s+|\s+$/gm, '');
}

/**
 *
 * @param daysOfWeek
 * @param period 0=am,1=pm,2=eve
 */
function unScheduleAll(col, daysOfWeek, period) {
    var $the = $(col);
    var isUnschedule = false;
    if ($the.is(":checked")) {
        isUnschedule = true;
    }

    var beginLessonTime = parseInt(lessonsPerDay) * daysOfWeek;
    var endLessonTime = 0;
    if (period == 0) {
        endLessonTime = beginLessonTime + parseInt(defaultAmLessonCount) - 1;
    } else if (period == 1) {
        beginLessonTime = beginLessonTime + parseInt(defaultAmLessonCount);
        endLessonTime = beginLessonTime + parseInt(defaultPmLessonCount) - 1;
    } else {
        beginLessonTime = beginLessonTime + parseInt(defaultAmLessonCount) + parseInt(defaultPmLessonCount);
        endLessonTime = beginLessonTime + parseInt(defaultEveLessonCount) - 1;
    }

    for (; beginLessonTime <= endLessonTime; beginLessonTime++) {
        var lessonTime = beginLessonTime;
        var $col = $("#td_" + lessonTime);
        if ($col.hasClass("disabledTd") || $col.attr("ctype") == "tag") {
            continue;
        }

        if (setRule(lessonTime, isUnschedule, "", "")) {
            if (isUnschedule) {
                $col.html("不排课");
            } else {
                $col.html("&nbsp;");
            }
            //
            setUnScheduleAll();
        } else {
            //
            setUnScheduleAll();
            return;
        }
    }

}
/**
 *
 */
function setUnScheduleAll() {
    for (var i = 0; i < defaultDaysPerWeek; i++) {
        var hasAllArr = [-1, -1, -1];
        for (var j = 0; j < lessonsPerDay; j++) {
            var lessonTime = i * lessonsPerDay + j;
            var period = getPeriod(j);
            var $td = $("#td_" + lessonTime);
            if ($td.hasClass("disabledTd") || $td.attr("ctype") == "tag") {
                continue;
            }

            var v = 0
            // 没有值
            if ($td.text().trim() == "") {
                v = 0;
            } else {
                v = 1;
            }

            if (hasAllArr[period] == -1) {
                hasAllArr[period] = v;
            } else {
                hasAllArr[period] = hasAllArr[period] & v;
            }
        }

        for (var period = 0; period < hasAllArr.length; period++) {
            var hasAll = hasAllArr[period] == 1;
            var $chk = $("#unScheduleAll_" + i + "_" + period);
            $chk.prop("checked", hasAll);
        }
    }
}
function getPeriod(lessonIndexOfDay) {
    if (lessonIndexOfDay < defaultAmLessonCount) {
        return 0;
    }

    if (lessonIndexOfDay >= defaultAmLessonCount
        && lessonIndexOfDay < defaultAmLessonCount + defaultPmLessonCount) {
        return 1;
    }


    return 2;
}

/**
 *
 * @param lessonTime
 */
function setUnschedule(lessonTime) {
    var td = $("#td_" + lessonTime);
    var text = trim(td.text());
    //if (td.text() == '不排课') {
    if (text.length > 0) {  // 如果格子里面有字，不排课或者tag，则去掉不排课的同时，顺便去掉tag记录
        if (setRule(lessonTime, false, selectedTagId, selectedTagName)) {
            td.html("&nbsp;");
            td.removeAttr("ctype");
        }
        // return;//不加出bug
    } else {  //加入不排课规则的同时 添加tag记录
        if (setRule(lessonTime, true, selectedTagId, selectedTagName)) {
            if (selectedTagName != "") {
                td.html(selectedTagName);
                td.attr("ctype", "tag");
            } else {
                td.html("不排课");
            }
        }
    }

    //
    setUnScheduleAll();
}



